// pages/orderEditAddress2/orderEditAddress2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],   //查询的收货地址列表
    radio: '',
    orderid:'',   //订单id
  },
  // 修改收货地址提交数据
  submit(){
    if(this.data.radio == ''){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      })
      return
    }else{
      var that = this;
      
      //点击获取addressid   地址id
      that.data.addressid = that.data.radio
      
      //当前页面
      let pages = getCurrentPages();
      
      //上一页面
      let prevPage = pages[pages.length - 2];
      //将数值信息赋值给上一页面cityItem变量
      prevPage.setData({
        addressid: that.data.addressid
      });
      
      //关闭当前界面返回到上一页面
      wx.navigateBack({
        delta: 1
      })
      
    }
    
  },
  // 修改选中的收货地址
  onChange(event) {
    // console.log('event.detail',event.detail)
    this.setData({
      radio: event.detail,
    });

  },
  
  // 查询收货地址列表
  getaddressList(){
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/select',
      data: {
        userid:getApp().globalData.openid,
        page:1,
        size:100
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        // console.log('res',res.data.data)
        if(res.data.flag){
          that.setData({
            addressList:res.data.data
          })
          var addlist = that.data.addressList
          for(var i = 0;i < addlist.length;i++){
            addlist[i].namefirst = addlist[i].name.slice(0,1)
          }
          that.setData({
            addressList:addlist
          })
          if(that.data.radio == ''){
            for(var i = 0; i< addlist.length;i++){
              if(addlist[i].prefer == '1'){
                that.setData({
                  radio:addlist[i].addressid
                })
              }
            }
          }
          // console.log('addressList',that.data.addressList)
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('optionsoptions',options.addressid)
    if(options.addressid != ''){
      this.setData({
        radio:options.addressid
      })
    }else{

    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getaddressList()
  },

})